package com.peng.community.service.impl;

import com.peng.community.entity.LoginTicket;
import com.peng.community.entity.User;
import com.peng.community.mapper.LoginTicketMapper;
import com.peng.community.mapper.UserMapper;
import com.peng.community.service.LoginTicketService;
import com.peng.community.service.UserService;
import com.peng.community.util.CommunityUtils;
import com.peng.community.util.RedisKeyUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@Service
public class LoginTicketServiceImpl implements LoginTicketService {

    @Autowired
    UserService userService;

    @Autowired
    UserMapper userMapper;

//    @Autowired
//    LoginTicketMapper loginTicketMapper;

    @Autowired
    RedisTemplate redisTemplate;

    @Override
    public Map<String, Object> login(String username, String password, int expireTime) {
        Map<String, Object> map = new HashMap<>();
        if (StringUtils.isBlank(username)) {
            map.put("usernameError", "用户名不能为空");
            return map;
        }
        if (StringUtils.isBlank(password)) {
            map.put("passwordError", "密码不能为空");
            return map;
        }
        User user = userMapper.selectByName(username);
        if (user == null) {
            map.put("usernameError", "用户名不存在");
            return map;
        }

        if (user.getStatus() == 0) {
            map.put("usernameError", "用户未激活");
            return map;
        }

        password = CommunityUtils.md5(password + user.getSalt());
        if (!user.getPassword().equals(password)) {
            map.put("passwordError", "密码错误");
            return map;
        }

        LoginTicket loginTicket = new LoginTicket();
        loginTicket.setStatus(0); //0表示生效
        loginTicket.setUserId(user.getId());
        loginTicket.setTicket(CommunityUtils.getUUID());
        loginTicket.setExpired(new Date(System.currentTimeMillis() + expireTime * 1000));
//        loginTicketMapper.insertLoginTicket(loginTicket);

        //重构：ticket对象存入redis中
        String ticketKey = RedisKeyUtil.getTicketKey(loginTicket.getTicket());
        redisTemplate.opsForValue().set(ticketKey,loginTicket);//redis设置了自动序列化器

        map.put("ticket", loginTicket.getTicket());
        return map;
    }

    @Override
    public void layout(String ticket) {
        //让这个凭证失效
//        loginTicketMapper.updateStatus(ticket,1);

        //重构：ticket对象更新状态
        String ticketKey = RedisKeyUtil.getTicketKey(ticket);
        LoginTicket loginTicket = (LoginTicket) redisTemplate.opsForValue().get(ticketKey);//redis设置了自动序列化器
        loginTicket.setStatus(1); //设置失效
        redisTemplate.opsForValue().set(ticketKey,loginTicket);

    }

    @Override
    public LoginTicket findLoginTicketByTicket(String ticket) {
//        return loginTicketMapper.selectByTicket(ticket);
        //重构：从redis获取ticket
        String ticketKey = RedisKeyUtil.getTicketKey(ticket);
        return (LoginTicket) redisTemplate.opsForValue().get(ticketKey);
    }

}
